<?
include("../webkit/include/simple_db.php");
include("session.php");
include("common.php");
include("ajax_base.php");

$db = new SimpleDB();

function echo_header($filename) {
	 header("Content-type: application/octet-stream"); 
	 header("Content-Disposition: attachment; filename=".iconv("utf-8","gbk",$filename));
	 header("Pragma: no-cache");
	 header("Expires: 0");  
}

if(isset($_GET['filter_client_name'])) {
	$_SESSION['filter_client_name'] = $_GET['filter_client_name'];
	die("ok");
}
if(isset($_GET['select_client_name'])) {
	$_SESSION['select_client_name'] = $_GET['select_client_name'];
	die("ok");
}
if(isset($_GET['check_only_person'])) {
	$_SESSION['check_only_person'] = $_GET['check_only_person'];
	die("ok");
}

if(isset($_GET['dl_excel'])) {
	echo_header("湖南电信外包呼叫中心SI商机激励明细_".$_SESSION['report_date_range'].".xls");
}


$date_range = split("_", $_SESSION['report_date_range']);
$month_total = "IF(order_date < STR_TO_DATE('".$date_range[0]."', '%Y-%m-%d'),  "; // 比较起始时间和签约时间; 2014/4/28
$month_total .= "ROUND(DATEDIFF(STR_TO_DATE('".$date_range[1]."', '%Y-%m-%d'), STR_TO_DATE('".$date_range[0]."', '%Y-%m-%d'))/30), "; // 计算时间范围内的月份
$month_total .= "ROUND(DATEDIFF(STR_TO_DATE('".$date_range[1]."', '%Y-%m-%d'), order_date)/30))"; // 计算从签约时间到截时间的月份

$agent_money_rate = "CASE client_source WHEN 1 THEN 0.15 ELSE 0.3 END";
if( $_SESSION['check_only_person'] == "true" ) {
	$agent_money_rate = "CASE client_source WHEN 1 THEN 0.1 ELSE 0.15 END";
}

$sql = "select client_name, client_type, sum(order_count) as order_count, count(a.id) as client_count, order_price, order_date, client_status, service_type, sum($agent_money_rate * order_count*order_price*$month_total) as agent_money_sum, sum(CASE client_source WHEN 1 THEN 0.3 ELSE 0.6 END * order_count*order_price*$month_total) as si_money_sum, $month_total as month_total, 
(CASE client_source WHEN 1 THEN 0.3 ELSE 0.6 END) as si_money_rate, ($agent_money_rate) as agent_money_rate, b.truename from hncc_client a Left Join hncc_user b On a.user_name=b.username  group by client_name having 1=1 order by order_date";

$condition = "client_status=1 and order_date < '".$date_range[1]."'";  // 这里只要判断签约日期在限之内即可; 2014/3/28

if( ! empty($_SESSION['filter_client_name']) ) {
	// 过滤掉部分未确认的分成项目; 2014/4/28
	$condition .= " and client_name not in('". implode("','", explode(",", $_SESSION['filter_client_name']))  ."')";
}
if( ! empty($_SESSION['select_client_name']) ) {
	// 只统计部分已选中的分成项目; 2014/5/4
	$condition .= " and client_name in('". implode("','", explode(",", $_SESSION['select_client_name']))  ."')";
}
$sql = str_replace("1=1", $condition, $sql);
$db->query($sql);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../webkit/utils/jquery.js"></script>
<link type="text/css" rel="stylesheet" href="page.css"/>
<style> td {font-size:15px; font-family:黑体} </style>
</head>
<body  style="margin:3px; overflow-y:scroll">

<table width=100% border=1 cellspacing=0 cellpadding=5 bgcolor="#999999">
	<tr bgcolor="#eeeeee" height=40>
	<td style="font-size:25px;" colspan=10>
		湖南电信外包呼叫中心SI商机激励明细
		<? if(empty($_GET['dl_excel'])) { ?>
		<span id="page_links">  　 
		<a href="<?echo $PHP_SELF."?dl_excel=1";?>">下载表格</a>　<a href="javascript:$('#page_links').hide(); window.print()">打印本页</a>　
		<input type='text' size=30 id="filter_client_name" value="<?echo $_SESSION['filter_client_name']?>"><a href="javascript:save_filter();">【过滤】</a>　
		<input type='text' size=30 id="select_client_name" value="<?echo $_SESSION['select_client_name']?>"><a href="javascript:save_select();">【选中】</a>&nbsp;
		<select id="check_only_person" onchange="save_check()">
			<option <? if($_SESSION['check_only_person']=="false") {echo "selected";} ?> value="false">代理</option>
			<option <? if($_SESSION['check_only_person']=="true") {echo "selected";} ?> value="true">个人</option>
		</select>
		</span>
		<?} else {
		 echo " (".$_SESSION['report_date_range'].")"; // 下载时输出时间范围
		}?>
    </td>
	</tr>
	<tr bgcolor='white' style='font-weight:bold'><td>客户名称</td><td>所属行业</td><td>租用数量</td><td>租用单价</td><td>签约时间</td><td>SI分成比例</td><td>商机来源</td><td>提成比例</td><td>提成月数</td><td>提成金额</td></tr>
	<?
		$order_count = 0;
		$si_money_sum = 0;
		$agent_money_sum = 0;
		while($db->moveNext()) {
			echo "<tr bgcolor='white'><td>".$db->f("client_name")."</td><td>".$db->f("client_type")."</td><td>".$db->f("order_count")."</td><td>".$db->f("order_price")."</td><td>".$db->f("order_date")."</td><td>".$db->f("si_money_rate")."</td><td>".$db->f("truename")."</td><td>".$db->f("agent_money_rate")."</td><td>".$db->f("month_total")."</td><td>".$db->f("agent_money_sum")."</td></tr>";
			$order_count += $db->f("order_count");
			$si_money_sum += $db->f("si_money_sum");
			$agent_money_sum += $db->f("agent_money_sum");
		}
	?>
	<tr bgcolor='white' style='font-weight:bold; color:red'><td>总计</td><td>&nbsp;</td><td><?echo $order_count?></td><td>&nbsp;</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp;</td><td>&nbsp;</td><td>¥ <?echo money_num_format($agent_money_sum)?></td></tr>
</table>
<script>
	function save_filter() {
		$.get("<?echo $_SERVER['PHP_SELF']?>", {'filter_client_name':$('#filter_client_name').val()}, function(data){
			if(data == "ok") {
				window.location.reload();
			}
		});
	}

	function save_select() {
		$.get("<?echo $_SERVER['PHP_SELF']?>", {'select_client_name':$('#select_client_name').val()}, function(data){
			if(data == "ok") {
				window.location.reload();
			}
		});
	}

	function save_check() {
		$.get("<?echo $_SERVER['PHP_SELF']?>", {'check_only_person':$('#check_only_person').val()}, function(data){
			if(data == "ok") {
				window.location.reload();
			}
		});
	}
</script>

</body>
</html>
